package com.suep.game_chat.controller;

import com.suep.game_chat.dto.GameCharacterDTO;
import com.suep.game_chat.dto.UserProfileDTO;
import com.suep.game_chat.entity.GameCharacter;
import com.suep.game_chat.entity.User;
import com.suep.game_chat.entity.UserPrivacy;
import com.suep.game_chat.service.UserProfileService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户档案控制器
 * 处理用户个人资料、隐私设置和游戏角色管理等功能
 *
 * @author Game Chat Team
 * @version 1.0
 */
@RestController
@RequestMapping("/userapi/users/profile")
@CrossOrigin(origins = "*")
public class UserProfileController {

    @Autowired
    private UserProfileService userProfileService;

    /**
     * 获取用户档案信息
     * 返回用户的基本信息和隐私设置
     *
     * @param userId 用户ID
     * @return ResponseEntity 包含用户信息和隐私设置
     */
    @GetMapping("/{userId}")
    public ResponseEntity<?> getProfile(@PathVariable String userId) {
        User user = userProfileService.getProfile(userId);
        UserPrivacy privacy = userProfileService.getPrivacySettings(userId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("user", user);
        response.put("privacy", privacy);
        return ResponseEntity.ok(response);
    }

    /**
     * 更新用户档案信息
     * 修改用户的基本信息
     *
     * @param userId 用户ID
     * @param profileDTO 更新的用户信息
     * @return ResponseEntity 包含更新后的用户信息
     */
    @PutMapping("/{userId}")
    public ResponseEntity<?> updateProfile(
            @PathVariable String userId,
            @Valid @RequestBody UserProfileDTO profileDTO) {
        User user = userProfileService.updateProfile(userId, profileDTO);
        return ResponseEntity.ok(user);
    }

    /**
     * 获取用户隐私设置
     *
     * @param userId 用户ID
     * @return ResponseEntity 包含用户的隐私设置信息
     */
    @GetMapping("/{userId}/privacy")
    public ResponseEntity<?> getPrivacySettings(@PathVariable String userId) {
        UserPrivacy privacy = userProfileService.getPrivacySettings(userId);
        return ResponseEntity.ok(privacy);
    }

    /**
     * 更新用户隐私设置
     *
     * @param userId 用户ID
     * @param profileDTO 更新的隐私设置信息
     * @return ResponseEntity 包含更新后的隐私设置
     */
    @PutMapping("/{userId}/privacy")
    public ResponseEntity<?> updatePrivacySettings(
            @PathVariable String userId,
            @Valid @RequestBody UserProfileDTO profileDTO) {
        UserPrivacy privacy = userProfileService.updatePrivacySettings(userId, profileDTO);
        return ResponseEntity.ok(privacy);
    }

    /**
     * 添加游戏角色
     * 为用户添加新的游戏角色
     *
     * @param userId 用户ID
     * @param characterDTO 游戏角色信息
     * @return ResponseEntity 包含新创建的角色信息
     */
    @PostMapping("/{userId}/characters")
    public ResponseEntity<?> addGameCharacter(
            @PathVariable String userId,
            @Valid @RequestBody GameCharacterDTO characterDTO) {
        GameCharacter character = userProfileService.addGameCharacter(userId, characterDTO);
        return ResponseEntity.ok(character);
    }

    /**
     * 更新游戏角色信息
     * 修改指定角色的属性
     *
     * @param userId 用户ID
     * @param characterId 角色ID
     * @param characterDTO 更新的角色信息
     * @return ResponseEntity 包含更新后的角色信息
     */
    @PutMapping("/{userId}/characters/{characterId}")
    public ResponseEntity<?> updateGameCharacter(
            @PathVariable String userId,
            @PathVariable String characterId,
            @Valid @RequestBody GameCharacterDTO characterDTO) {
        GameCharacter character = userProfileService.updateGameCharacter(userId, characterId, characterDTO);
        return ResponseEntity.ok(character);
    }

    /**
     * 获取用户的所有游戏角色
     *
     * @param userId 用户ID
     * @return ResponseEntity 包含用户所有角色的列表
     */
    @GetMapping("/{userId}/characters")
    public ResponseEntity<?> getUserCharacters(@PathVariable String userId) {
        List<GameCharacter> characters = userProfileService.getUserCharacters(userId);
        return ResponseEntity.ok(characters);
    }

    /**
     * 删除游戏角色
     *
     * @param userId 用户ID
     * @param characterId 要删除的角色ID
     * @return ResponseEntity 包含删除成功的确认信息
     */
    @DeleteMapping("/{userId}/characters/{characterId}")
    public ResponseEntity<?> deleteGameCharacter(
            @PathVariable String userId,
            @PathVariable String characterId) {
        userProfileService.deleteGameCharacter(userId, characterId);
        Map<String, Object> response = new HashMap<>();
        response.put("message", "角色删除成功");
        return ResponseEntity.ok(response);
    }
} 